
#include <cassert>
#include <cstring>
#include <iostream>
#include <mpi.h>
using namespace std;

int main(int argc, char **argv) {
  int rank, num_process;
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &num_process);

  constexpr int mess_length{100};
  char message[mess_length];

  if (rank != 0) {
    MPI_Request req;
    MPI_Status status;
    sprintf(message, "greeting from process %d", rank);
    MPI_Isend(message, strlen(message), MPI_CHAR, 0, 1, MPI_COMM_WORLD, &req);
    MPI_Waitall(1, &req, &status);
  } else {
    MPI_Request *req = new MPI_Request[num_process];
    MPI_Status *status = new MPI_Status[num_process];
    for (int i = 1; i < num_process; ++i) {
      MPI_Irecv(message, mess_length, MPI_CHAR, i, 1, MPI_COMM_WORLD,
                &req[i - 1]);
    }

    MPI_Waitall(num_process, req, status);
    printf("%s\n", message);
  }

  MPI_Finalize();

  return 0;
}